
package com.idealighter.game.games.tongbiniuniu.struct;

import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.common.niuniu.NiuNiuCard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * . 通比牛牛桌子 .
 * 
 * @date 2016年2月17日 上午10:46:55
 */
public class TongBiNiuNiuTable extends AbstractTable {

  // 座位数量
  private static final int SEAT_NUM = 4;

  public final TongBiNiuNiuRoom room;
  public final List<TongBiNiuNiuSeat> seats;
  // 桌子是否准备完成
  public boolean readied = false;
  // 桌上的总筹码
  public long totalChips = 0;

  // 一副牌
  public final List<NiuNiuCard> cards = Arrays.asList(NiuNiuCard.values());

  /**
   * 构造函数 .
   * 
   * @param id .
   * @param room .
   */
  public TongBiNiuNiuTable(int id, TongBiNiuNiuRoom room) {
    super(id);
    this.room = room;
    this.seats = createSeats();
  }

  /**
   * 创建位置.
   * 
   * @return
   */
  private List<TongBiNiuNiuSeat> createSeats() {
    List<TongBiNiuNiuSeat> seats = new ArrayList<>(SEAT_NUM);
    for (int i = 0; i < SEAT_NUM; i++) {
      seats.add(new TongBiNiuNiuSeat(i, this));
    }

    return Collections.unmodifiableList(seats);
  }

  /**
   * 重置桌子 .
   */
  public void reset() {
    this.readied = false;
    this.totalChips = 0;
    Collections.shuffle(cards);
    Collections.shuffle(cards);
  }

  /**
   * 闲家合计筹码 .
   * 
   * @return
   */
  public long playesChips() {
    long playesChips = 0;
    for (TongBiNiuNiuSeat s : seats) {
      if (!s.dealer && s.getPlayerId() > 0) {
        playesChips += s.getTotalChips();
      }
    }

    return playesChips;
  }

  /**
   * 叫庄座位 .
   * 
   * @return
   */
  public List<TongBiNiuNiuSeat> callDealerSeats() {
    // 叫庄玩家
    List<TongBiNiuNiuSeat> callDealerSeats = new ArrayList<>();
    // 叫过庄的玩家
    List<TongBiNiuNiuSeat> calledDealerSeats = new ArrayList<>();
    for (TongBiNiuNiuSeat s : seats) {
      if (s.callDealer != null) {
        calledDealerSeats.add(s);
        if (s.callDealer) {
          callDealerSeats.add(s);
        }
      }
    }

    if (callDealerSeats.size() == 0) { // 没有人叫庄等于所有人叫庄
      callDealerSeats = calledDealerSeats;
    }

    return callDealerSeats;
  }

  /**
   * 牌桌是否全部摊牌 .
   * 
   * @return
   */
  public boolean showdown() {
    for (TongBiNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0 && !s.showdown) {
        return false;
      }
    }

    return true;
  }

  /**
   * 叫庄是否结束, 所有人都叫过庄且人数超过1个.
   * 
   * @return
   */
  public boolean callDealerOver() {
    long callDealerNum = 0;
    for (TongBiNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0) {
        if (s.callDealer == null) {
          return false;
        }
        callDealerNum++;
      }
    }

    if (callDealerNum <= 1) {
      return false;
    }

    return true;
  }

  /**
   * 牌桌是否下注结束 .
   * 
   * @return
   */
  public boolean betOver() {
    TongBiNiuNiuSeat dealer = dealer();
    for (TongBiNiuNiuSeat s : seats) {
      if (s.getPlayerId() > 0 && s.getPlayerId() != dealer.getPlayerId() && s.betedchips == 0) {
        return false;
      }
    }

    return true;
  }

  /**
   * 返回庄家 .
   * 
   * @return
   */
  public TongBiNiuNiuSeat dealer() {
    for (TongBiNiuNiuSeat seat : seats) {
      if (seat.dealer) {
        return seat;
      }
    }

    return null;
  }

  /**
   * 获取seat .
   * 
   * @param order .
   * @return
   */
  public TongBiNiuNiuSeat getSeat(int order) {

    return seats.get(order);
  }

  @SuppressWarnings("unchecked")
  @Override
  public TongBiNiuNiuRoom room() {
    return room;
  }

  @Override
  public List<TongBiNiuNiuSeat> seats() {

    return seats;
  }

}
